<?
function addpost($data,$parent,$rssid=0,$userID=1)
{
	$data["title"]=trim($data["title"]);
	#####ADD TAGS########
	$data["tags"]=trim($data["tags"]);
	$tags=explode(",",$data["tags"]);
	for($i=0; $i<count($tags); $i++) $tags[$i]=trim($tags[$i]);
	$data["tags"]=implode(",",$tags);
	$arr_tags=str_replace(",","'),('",$data["tags"]);
		$q=query("INSERT IGNORE INTO tags (code) VALUES ('$arr_tags');");
	##################
	//$data["image"]=file_get_contents(SERVERIMG."/leechimage.php?img=$data[image]");
	$q=query("select * from post where image='$data[image]'");
	if(numrows($q)>0) return;
	
		$size = getimagesize($data["image"]);
	$width			= $size[0];
	$height			= $size[1];
	$data1=array(
				"userid" => $userID,
				"parent" => $parent,
				"title" => trim($data["title"]),
				"time" => $data["pubDate"],
				"image" => $data["image"],
				"imgheight" =>$height,
				"imgwidth" => $width,
				"described" => trim($data["described"]),
				"content" => trim($data["content"]),
				"is_active"=>"1",
				"trailer"=>"",
				"tags" => $data["tags"]
			);
	$ret = insert($data1,"post");
			if($ret)
				{
					$id=get_insert_id();
					writelog($data1,$rssid,$id);
					echo $data1["title"]."--".$id."<br>";
				}
			else
				{
					writelog($data1,$rssid);
					echo $data1["title"]."-- error<br>";
				}
}
function writelog($datai,$rssid=0,$id=0)
{
	$data=array(
		"postid" => $id,
		"title" => $datai["title"],
		"time" => $datai["time"],
		"rssid" => $rssid,
	);
	insert($data,"rsslogs");
}
function ngoisaovn($rss) {    
    $x=file_get_contents($rss["url"]);
preg_match_all("/a class=\"aitem-boxnew\" href=\"(.*?)\"/", $x,$link);
preg_match_all("/span class=\"title_boxnew\">(.*?)<\/span>/", $x,$title);
for($i=0; $i<count($link[1])-1; $i++)
{
    $data["title"]=$title[1][$i];
    $data["link"]=$link[1][$i];
    $x=file_get_contents("http://m.ngoisao.vn/".$link[1][$i]);
 //   echo $x;
    preg_match("/<div class=\"timeleft\">(.*?)<\/div>/s", $x, $time);
    $data["pubDate"]=strtotime(str_replace("/","-", $time[1]));
    if($data["pubDate"] < $rss["lasttime"]) break;
    preg_match("/<h2 class=\"head-newct\">(.*?)<\/h2>/s", $x, $desc);
    $data["described"]=$desc[1];
    
   #############GET CONTENT###############
    $options = array(
              'http'=>array(
                'method'=>"GET",
                'header'=>"Accept-language: en\r\n" .
                          "Cookie: foo=bar\r\n" .  // check function.stream-context-create on php.net
                          "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad 
              )
            );

    $context = stream_context_create($options);
    $content = file_get_contents("http://ngoisao.vn/".$link[1][$i], false, $context);
    preg_match("/<div class=\"detail\-content\">(.*?)<\/div>/s", $content, $out);
    $contentDetail=preg_replace("/<a href=\"http:\/\/ngoisao.vn\/\">(.*?)<\/a>/", "$1", $out[1]);
    $contentDetail=preg_replace("/<iframe(.*?)>(.*?)<\/iframe>/", "", $contentDetail);
    $data["content"]=$contentDetail;
    ##########GET IMAGE##################
    preg_match("/<img(.*?)src=\"(.*?)\"(.*?)>/si", $contentDetail,$out);
    $data["image"]=$out[2];
    preg_match("/<div class=\"ct\-tags\"(.*?)>(.*?)<\/div>/s", $content, $out);
    $tags=$out[2];
    $tags=str_replace("<strong>Tags</strong>","",$tags);
    $tags=preg_replace("/<a href=\"(.*?)\">(.*?)<\/a>/", "$2", $tags);
    $tags=str_replace(", ",",",$tags);
    $data["tags"]=$tags;
    
     if($data["image"]!="" && $data["title"]!="" && $data["content"]!="" &&$data["described"]!="")
    $ret[]=$data;
//echo $deatail[1];
}
return $ret;
}
function ngoisaonet($rss)
{
	 $content1 = file_get_contents($rss["url"]);
    $x = new SimpleXmlElement($content1);
      
    foreach($x->channel->item as $entry) {
	$data=array();
	if(strtotime($entry->pubDate)<$rss["lasttime"]) break;
	
	$data["title"]=strip_tags($entry->title);
	$data["described"]=strip_tags($entry->description);
	$data["link"]=strip_tags($entry->link);
	$data["pubDate"]=strtotime($entry->pubDate);
	##########GET CONTENT###############
	$content=file_get_contents($data["link"]);
	preg_match("/<div class=\"fck_detail\">(.*?)<\/div>/s", $content, $out);
	$data["content"]=$out[1];
	##########GET IMAGE##################
	preg_match("/<img(.*?)src=\"(.*?)\"(.*?)>/si", $data["content"],$out);
	$data["image"]=$out[2];
	##########GET TAGS###################
	preg_match("/<div class=\"wordTag\"(.*?)>(.*?)<\/div>/s", $content, $out);
	
	preg_match_all("/<a(.*?)>(.*?)<\/a>/", $out[2],$out1);
	$data["tags"]=implode(",",$out1[2]);
	if($data["image"]!="" && $data["title"]!="" && $data["content"]!="" &&$data["described"]!="")
	$ret[]=$data;
	}
	return $ret;
}
?>